export default class Http {
    static ajax(op) {
        const options = {
            url: op.url,
            method: op.method || 'POST',
            success: op.success,
            error: op.error,
            data: op.data,
            contentType: op.contentType || 'application/json',
            timeout: op.timeout || 5000,
        }

        const xhr = new XMLHttpRequest();

        xhr.timeout = options.timeout;

        xhr.open(options.method, options.url, true);
        xhr.setRequestHeader("Content-type", options.contentType);

        xhr.send(options.data);

        xhr.onreadystatechange = (data) => {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    options.success && options.success(data.currentTarget);
                } else {
                    options.error && options.error(data);
                }
            }
        }
    }
}
